package com.king.tv.mvp.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hannesdorfmann.mosby.mvp.MvpPresenter;
import com.king.tv.MainActivity;
import com.king.tv.R;
import com.king.tv.bean.AddUserBean;
import com.king.tv.dao.greendao.bean.User;
import com.king.tv.dao.greendao.dao.UserDao;
import com.king.tv.dao.greendao.manager.ManagerFactory;
import com.king.tv.http.BaseCallModel;
import com.king.tv.http.HttpLoader;
import com.king.tv.mvp.base.BaseActivity;
import com.king.tv.util.SPUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class PromotersActivity extends BaseActivity {

    @BindView(R.id.ivLeft)
    ImageView ivLeft;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.ivRight)
    ImageView ivRight;
    @BindView(R.id.et_bingnum)
    EditText etBingnum;
    @BindView(R.id.btn_bindnum)
    Button btnBindnum;

    @Override
    public int getRootViewId() {
        return R.layout.activity_promoter;
    }

    @Override
    public void initUI() {
        ivLeft.setVisibility(View.GONE);
        ivRight.setVisibility(View.GONE);
        tvTitle.setText("绑定推广员");
    }

    @Override
    public void initData() {
        getUserid();
    }

    private void getUserid() {
        HttpLoader httpLoader = new HttpLoader();
        httpLoader.addUser().subscribe(new Observer<AddUserBean>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.i("tag","=========Result"+d.toString());
            }

            @Override
            public void onNext(AddUserBean value) {
                Log.i("tag","=========Result"+value.getData().getId().toString());
                Gson gson = new Gson();
                String json = gson.toJson(value.getData());
                User user =gson.fromJson(json,User.class);
                ManagerFactory.getInstance().getStudentManager().save(user);
                Log.i("tag","=========Result"+json);
            }

            @Override
            public void onError(Throwable e) {
                Log.i("tag","=========Result"+e.toString());
            }

            @Override
            public void onComplete() {
                Log.i("tag","=========Result end");
            }
        });
    }

    @NonNull
    @Override
    public MvpPresenter createPresenter() {
        return null;
    }


    @OnClick(R.id.btn_bindnum)
    public void onViewClicked(){
        List<User> users = ManagerFactory.getInstance().getStudentManager().queryAll();
        if(users.size()>0){
            User user = users.get(0);
            String code = etBingnum.getText().toString().trim();
            HttpLoader.getInstance().getBind(user.getId(),code).subscribe(new Observer<Object>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onNext(Object value) {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onComplete() {

                }
            });
        }

    }
}
